﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Configuration;

namespace Trade.Library.Data
{
    public class ConnectionPool
    {
        static List<ConnectionModel> lstConn = new List<ConnectionModel>();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionStringName"></param>
        /// <returns></returns>
        public static IDbConnection GetConnection(IDbConnection conn)
        {
            lstConn.RemoveAll(r => r.conn == null || r.connString == null || string.IsNullOrEmpty(r.connString));
            ConnectionModel model = new ConnectionModel() { conn = conn, KeyID = Guid.NewGuid().ToString() };
            if (lstConn.Exists(r => r.conn == conn && r.connState == ConnectionState.Closed))
            {
                IDbConnection con = lstConn.Find(r => r.conn == conn && r.connState == ConnectionState.Closed).conn;
                con.Open();
                return con;
            }
            else
            {
                lstConn.Add(model);
                IDbConnection con = model.conn;
                con.Open();
                return con;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connstring"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        public static IDbConnection GetConnection(string connstring, DbProviderFactory provider)
        {
            //lstConn.RemoveAll(r => r.conn == null || r.connString == null || string.IsNullOrEmpty(r.connString));
            //if (lstConn.Exists(r => r.connString == connstring && r.DBType == GetDBType(provider) && r.connState == ConnectionState.Closed))
            //lstConn.RemoveAll(r => r.conn == null);
            //if (lstConn.Exists(r => r.DBType == GetDBType(provider) && r.connState == ConnectionState.Closed))
            //{
            //    IDbConnection con = lstConn.Find(r => r.DBType == GetDBType(provider) && r.connState == ConnectionState.Closed).conn;
            //    if (con.State == ConnectionState.Closed)
            //    {
            //        con.ConnectionString = connstring;
            //        con.Open();

            //    }
            //    else
            //    {
            //        con = provider.CreateConnection();
            //        con.ConnectionString = connstring;
            //        lstConn.Add(new ConnectionModel() { conn = con, KeyID = Guid.NewGuid().ToString() });
            //        con.Open();
            //    }
            //    return con;

            //}
            //else
            //{
                IDbConnection _Connection = provider.CreateConnection();
                _Connection.ConnectionString = connstring;
                //lstConn.Add(new ConnectionModel() { conn = _Connection, KeyID = Guid.NewGuid().ToString() });
                _Connection.Open();
                return _Connection;
            //}
        }

        private static DataBaseType GetDBType(DbProviderFactory provider)
        {
            string dbtype = provider.GetType().Name;
            if (dbtype.Contains("MySql")) return DataBaseType.MySql;
            else if (dbtype.Contains("System.Data.SqlClient")) return DataBaseType.SqlServer;
            else return DataBaseType.SqlServer;
        }

    }

    /// <summary>
    /// 连接字符串Model
    /// </summary>
    public class ConnectionModel
    {
        /// <summary>
        /// 连接
        /// </summary>
        public IDbConnection conn { get; set; }

        /// <summary>
        /// 连接字符串状态
        /// </summary>
        public ConnectionState connState { get { return conn.State; } }

        /// <summary>
        /// 连接字符串
        /// </summary>
        public string connString { get { return conn.ConnectionString; } }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public DataBaseType DBType
        {
            get
            {
                string dbtype = conn.GetType().Name;
                if (dbtype.Contains("MySql")) return DataBaseType.MySql;
                else if (dbtype.Contains("System.Data.SqlClient.")) return DataBaseType.SqlServer;
                else return DataBaseType.SqlServer;
            }
        }

        /// <summary>
        /// 当前Model的ID值
        /// </summary>
        public string KeyID { get; set; }
    }
}
